﻿using CK.Sprite.Form.Core;
using CK.Sprite.Framework;
using Dapper;
using Dapper.Contrib.Extensions;
using JetBrains.Annotations;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CK.Sprite.Form.MySql
{
    public class SpriteCommonRepository : BaseSpriteRepository<SpriteObject>, ISpriteCommonRepository
    {
        public SpriteCommonRepository(IUnitOfWork unitOfWork) : base(unitOfWork) { }

        public async Task<List<T>> GetCommonList<T>(string tableName, List<QueryWhereModel> queryWhereModels, string fields = default)
        {
            JObject paramValues = new JObject();
            string strSql = $"SELECT {(string.IsNullOrEmpty(fields) ? "*" : fields)} FROM {tableName} WHERE {CreateSqlWhere(queryWhereModels, paramValues)};";
            var result = await _unitOfWork.Connection.QueryAsync<T>(strSql, paramValues.ToConventionalDotNetObject());
            return result.ToList();
        }
    }
}
